//
//  DownView.m
//  同盟圈
//
//  Created by 洛水寒 on 16/4/20.
//  Copyright © 2016年 ym. All rights reserved.
//
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#import "DownView.h"

@implementation DownView
{
    NSMutableArray *titleaaaArray;
    UIView *bgView;
    int tag;
}

- (id)initWithFrame:(CGRect)frame titleArray:(NSMutableArray *)titleArray withTag:(int)tags
{
    self = [super initWithFrame:frame];
    if (self) {
        titleaaaArray = titleArray;

        tag = tags;
    _tableView= [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = [UIColor groupTableViewBackgroundColor];
    _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        
        _tableView.rowHeight = 30;
    [self addSubview:_tableView];

    }
    return self;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titleaaaArray.count;
}
//重用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:cellID];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [titleaaaArray objectAtIndex:indexPath.row];;
    cell.textLabel.font=[UIFont systemFontOfSize:13];
    cell.textLabel.textColor = [UIColor blueColor];
    switch (tag) {
        case 1:
        {
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
        }
            break;
        case 2:
        {
            cell.textLabel.textAlignment = NSTextAlignmentLeft;
        }
            break;
            
        default:
            break;
    }

    
    return cell;
}
//点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.clickedBtnIndexBlock(indexPath.row);
    [bgView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
   // self.center = window.center;
    bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgView.backgroundColor = [UIColor clearColor];
    
    [window addSubview:bgView];
    [window addSubview:self];
    
    
    UITapGestureRecognizer *recognizerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(handleTapBehind:)];
    
    [recognizerTap setNumberOfTapsRequired:1];
    recognizerTap.cancelsTouchesInView = NO;
    [bgView addGestureRecognizer:recognizerTap];
}

- (void)handleTapBehind:(UITapGestureRecognizer *)sender{
    if (sender.state == UIGestureRecognizerStateEnded){
        CGPoint location = [sender locationInView:nil];
        
        if (![self pointInside:[self convertPoint:location fromView:self.window] withEvent:nil]){
            [bgView removeFromSuperview];
            [self removeFromSuperview];
            //[self dismissWithClickedButtonIndex:0 animated:YES];
        }
    }
}



@end
